package com.blrs.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.blrs.VO.AnswerVO;
import com.blrs.common.core.page.PageDomain;
import com.blrs.common.core.page.TableSupport;
import com.blrs.common.utils.page.PaginationUtil;
import com.blrs.service.questionnaire.IQuestionnaireEnrollService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.domain.QuestionnaireEnroll;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 问卷调查作答Controller
 * 
 * @author yang
 * @date 2023-12-01
 */
@RestController
@RequestMapping("/questionnaire/enroll")
public class QuestionnaireEnrollController extends BaseController {
    @Autowired
    private IQuestionnaireEnrollService questionnaireEnrollService;

    /**
     * 查询问卷调查作答列表
     */
    @GetMapping("/list")
    public TableDataInfo list(QuestionnaireEnroll questionnaireEnroll) {
        startPage();
        List<QuestionnaireEnroll> list = questionnaireEnrollService.selectQuestionnaireEnrollList(questionnaireEnroll);
        return getDataTable(list);
    }

    /**
     * 导出问卷调查作答列表
     */
    @Log(title = "问卷调查作答", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionnaireEnroll questionnaireEnroll) {
        List<QuestionnaireEnroll> list = questionnaireEnrollService.selectQuestionnaireEnrollList(questionnaireEnroll);
        ExcelUtil<QuestionnaireEnroll> util = new ExcelUtil<QuestionnaireEnroll>(QuestionnaireEnroll.class);
        util.exportExcel(response, list, "问卷调查作答数据");
    }

    /**
     * 获取问卷调查作答详细信息
     */
    @GetMapping(value = "/{answerId}")
    public AjaxResult getInfo(@PathVariable("answerId") String answerId) {
        return success(questionnaireEnrollService.selectQuestionnaireEnrollByAnswerId(answerId));
    }

    /**
     * 新增问卷调查作答
     */
    @Log(title = "问卷调查作答", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionnaireEnroll questionnaireEnroll) {
        return toAjax(questionnaireEnrollService.insertQuestionnaireEnroll(questionnaireEnroll));
    }

    /**
     * 修改问卷调查作答
     */
    @Log(title = "问卷调查作答", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionnaireEnroll questionnaireEnroll) {
        return toAjax(questionnaireEnrollService.updateQuestionnaireEnroll(questionnaireEnroll));
    }

    /**
     * 删除问卷调查作答
     */
    @Log(title = "问卷调查作答", businessType = BusinessType.DELETE)
    @DeleteMapping("/{answerIds}")
    public AjaxResult remove(@PathVariable String[] answerIds) {
        return toAjax(questionnaireEnrollService.deleteQuestionnaireEnrollByAnswerIds(answerIds));
    }

    /**
     * 保存问卷
     */
    @PostMapping("/saveValues/{questionnaireId}")
    public AjaxResult saveSelectedValues(@RequestBody List<Object> yourData,@PathVariable(name = "questionnaireId")String questionnaireId) throws JsonProcessingException {
        List<List<Integer>> result = new ArrayList<>();

        for (Object obj : yourData) {
            if (obj instanceof Integer) {
                // 创建新的嵌套列表
                List<Integer> nestedList = new ArrayList<>();
                nestedList.add((Integer) obj);
                result.add(nestedList);
            } else if (obj instanceof List) {
                // 处理嵌套数组
                List<Integer> nestedList = (List<Integer>) obj;
                result.add(nestedList);
            }
        }
       return questionnaireEnrollService.insertQuestionnaireEnrollByAnswer(result,questionnaireId);
    }

    /**
     * 根据问卷id查询问卷作答列表
     */
    @GetMapping("/isAnswer")
    public TableDataInfo isAnswerList( QuestionnaireEnroll questionnaireEnroll){
        startPage();
         List<QuestionnaireEnroll> questionnaireId=questionnaireEnrollService.selectIsAnswerListByQuestionnaireId(questionnaireEnroll);
        return getDataTable(questionnaireId);
    }
}